﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using WebAPI.Entities;

namespace WebAPI.Dao;

public class ClassifyMapper
{
    private SqlCommandExecutor _sqlCommandExecutor = new SqlCommandExecutor();
    
    public List<Classify> SelectAll()
    {
        List<Classify> list = new List<Classify>();
        Classify classify = null;

        string sql = "select * from [classify];";
        
        try
        {
            SqlDataReader sqlDataReader = _sqlCommandExecutor.Execute(sql);
            
            while (sqlDataReader.Read())
            {
                string id = (string)sqlDataReader["id"];
                string father_id = (string)sqlDataReader["father_id"];
                string name = (string)sqlDataReader["name"];
                string describe = (string)sqlDataReader["describe"];
            
                classify = new Classify(id, father_id, name, describe);
                list.Add(classify);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
        finally
        {
            _sqlCommandExecutor.CloseConnection();
        }
        
        return list;
    }

    public int Insert(Classify classify)
    {
        string sql = "insert [classify](id, father_id, name, describe) values(@id, @father_id, @name, @describe);";

        SqlCommand sqlCommand = _sqlCommandExecutor.PrepareSqlCommand(sql);
        
        sqlCommand.Parameters.AddWithValue("id", classify.Id);
        sqlCommand.Parameters.AddWithValue("father_id", classify.FatherId);
        sqlCommand.Parameters.AddWithValue("name", classify.Name);
        sqlCommand.Parameters.AddWithValue("describe", classify.Describe);
        
        int flag = 0;
        
        try
        {
            flag = sqlCommand.ExecuteNonQuery();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
        finally
        {
            _sqlCommandExecutor.CloseConnection();
        }
        
        return flag;
    }
    
    public int Update(Classify classify)
    {
        string sql = "update [classify] set id=@id, father_id=@father_id, name=@name, describe=@describe;";

        SqlCommand sqlCommand = _sqlCommandExecutor.PrepareSqlCommand(sql);
        
        sqlCommand.Parameters.AddWithValue("id", classify.Id);
        sqlCommand.Parameters.AddWithValue("father_id", classify.FatherId);
        sqlCommand.Parameters.AddWithValue("name", classify.Name);
        sqlCommand.Parameters.AddWithValue("describe", classify.Describe);
        
        int flag = 0;
        
        try
        {
            flag = sqlCommand.ExecuteNonQuery();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
        finally
        {
            _sqlCommandExecutor.CloseConnection();
        }
        
        return flag;
    }
    
    public int Delete(Classify classify)
    {
        string sql = "delete from [classify] where id=@id;";

        SqlCommand sqlCommand = _sqlCommandExecutor.PrepareSqlCommand(sql);
        
        sqlCommand.Parameters.AddWithValue("id", classify.Id);
        
        int flag = 0;
        
        try
        {
            flag = sqlCommand.ExecuteNonQuery();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
        finally
        {
            _sqlCommandExecutor.CloseConnection();
        }
        
        return flag;
    }
}